﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Utility;
using DTO;
using DAL;
using BUS;

using System.Data.SqlClient;
public partial class Doanh_Nghiệp_Doanhnghiep_Mathang_insert : System.Web.UI.Page
{
    public int id;
    protected void Page_Load(object sender, EventArgs e)
    { 
        
        if (!IsPostBack)
        {
            Label1.Text = Session["madn"].ToString();
            LoadDropDownList();


        }
    }
    public bool IsNumeric(string val)
    {
        double result;
        return double.TryParse(val, System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.CurrentInfo, out result);
    }
    public Object maxmh()
    {
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
        cn.Open();
        SqlCommand cmd = new SqlCommand("select max(mamh) from mathang", cn);
        return cmd.ExecuteScalar();


    }
    private void LoadDropDownList()
    {
        tbKhuVucCollection khuvuc = KHUVUC_BUS.GetAll();
        ddl_Khuvuc.DataTextField = tbKhuVucColumns.TenKhuVuc.ToString();
        ddl_Khuvuc.DataValueField = tbKhuVucColumns.MaKhuVuc.ToString();
        ddl_Khuvuc.DataSource = khuvuc;
        ddl_Khuvuc.DataBind();
        ddl_Khuvuc.Items.Insert(0, new ListItem("All", "0"));

        LoaiHangColection loaihang = LOAIHANG_BUS.GetAll();
        ddl_LoaiSP.DataTextField = LOAIHANGCOLUMNS.TenLoai.ToString();
        ddl_LoaiSP.DataValueField = LOAIHANGCOLUMNS.MaLoai.ToString();
        ddl_LoaiSP.DataSource = loaihang;
        ddl_LoaiSP.DataBind();
        ddl_LoaiSP.Items.Insert(0, new ListItem("All", "0"));

        //tbDoanhNghiepCollection doanhnghiep = DoanhNghiep_BUS.GetAll();
        //ddl_DoanhNghiep.DataTextField = tbDoanhNghiepColumns.TenDN.ToString();
        //ddl_DoanhNghiep.DataValueField = tbDoanhNghiepColumns.MaDN.ToString();
        //ddl_DoanhNghiep.DataSource = doanhnghiep;
        //ddl_DoanhNghiep.DataBind();
        //ddl_DoanhNghiep.Items.Insert(0, new ListItem("All", "0"));

        TinhTrang_SanPhamCollection tinhtrang_sp = TinhTrang_SanPham_BUS.GetAll();
        ddl_tinhtrang.DataTextField = TinhTrang_SanPhamColumns.TenTinhTrang.ToString();
        ddl_tinhtrang.DataValueField = TinhTrang_SanPhamColumns.MaTinhTrang_SP.ToString();
        ddl_tinhtrang.DataSource = tinhtrang_sp;
        ddl_tinhtrang.DataBind();
        ddl_tinhtrang.Items.Insert(0, new ListItem("All", "0"));

    }
  
    protected void btn_them_Click(object sender, EventArgs e)
    {


        MatHang_DTO mathang = new MatHang_DTO();
        mathang.TenMH = lit_TenSP.Text;
        //if (lit_TenSP.Text == "")
        //{
        //    lbtensanpham.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbtensanpham.Visible = false;
        //}
        //#region"kiem tra loai"
        //if (ddl_LoaiSP.SelectedValue == "0")
        //{
        //    lbloaisanpham.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbloaisanpham.Visible = false;
        //}
        //#endregion
        //#region "kiem tra dongia"
        //if (IsNumeric(lit_dongia.Text) == false)
        //{
        //    lbdongia.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbdongia.Visible = false;
        //}
        //if (lit_dongia.Text == "")
        //{
        //    lbdongia.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbdongia.Visible = false;
        //}
        //#endregion
        //#region "kiem tra so luong phieu ban"
        //if (IsNumeric(lit_slphieuban.Text) == false)
        //{
        //    lbsoluongphieuban.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbsoluongphieuban.Visible = false;
        //}
        //if (lit_slphieuban.Text == "")
        //{
        //    lbsoluongphieuban.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbsoluongphieuban.Visible = false;
        //}
        //#endregion
        //#region "kiem tra giảm giá"
        //if (IsNumeric(lit_giamgia.Text) == false)
        //{
        //    lbgiam.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbgiam.Visible = false;
        //}
        //if (lit_giamgia.Text == "")
        //{
        //    lbgiam.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbgiam.Visible = false;
        //}
        //#endregion
        //#region "kiem tra phí giao hàng"
        //if (IsNumeric(lit_phigiaohang.Text) == false)
        //{
        //    lbphigiaohang.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbphigiaohang.Visible = false;
        //}
        //if (lit_phigiaohang.Text == "")
        //{
        //    lbphigiaohang.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbphigiaohang.Visible = false;
        //}
        //#endregion
        //#region "kiem tra chiết khấu"
        //if (IsNumeric(lit_chietkhau.Text) == false)
        //{
        //    lbchietkhau.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbchietkhau.Visible = false;
        //}
        //if (lit_chietkhau.Text == "")
        //{
        //    lbchietkhau.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbchietkhau.Visible = false;
        //}
        //#endregion
        //#region"kiem tra doanh nghiệp"
        //if (ddl_DoanhNghiep.SelectedValue == "0")
        //{
        //    lbdoanhnghiep.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbdoanhnghiep.Visible = false;
        //}
        //#endregion
        //#region"kiem tra khu vực"
        //if (ddl_Khuvuc.SelectedValue == "0")
        //{
        //    lbkhuvuc.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbkhuvuc.Visible = false;
        //}
        //#endregion
        //#region"kiem tra tinh trạng sản phẩm"
        //if (ddl_tinhtrang.SelectedValue == "0")
        //{
        //    lbtinhtrang.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbtinhtrang.Visible = false;
        //}
        //#endregion
        //#region"kiểm tra mô tả"
        //if (lit_mota.Text == "")
        //{
        //    lbmota.Visible = true;
        //    return;
        //}
        //else
        //{
        //    lbmota.Visible = false;
        //}
        //#endregion


        int manhanvien;
        //manhanvien = int.Parse(Session["manv"].ToString());
        
        mathang.GiaBan = float.Parse(lit_dongia.Text);

        mathang.SoLuongPhieuBan = int.Parse(lit_slphieuban.Text);
        mathang.SoLuongPhieuConLai = int.Parse(lit_slphieuban.Text);
        mathang.TiLeGiam = float.Parse(lit_giamgia.Text);
        mathang.MoTa = "chua  duoc mo ta";
        mathang.PhiGiaoHangTanNoi = int.Parse(lit_phigiaohang.Text);
        mathang.PhanTram_ChietKhau = float.Parse(lit_chietkhau.Text);
        mathang.MaLoai = int.Parse(ddl_LoaiSP.SelectedValue);
        //int madoanhnghiep = int.Parse(Session["madn"].ToString());
        //madn.Value = Session["madn"].ToString();
        mathang.MaKhuVuc = int.Parse(ddl_Khuvuc.SelectedValue);
        mathang.MaTinhTrang_SP = int.Parse(ddl_tinhtrang.SelectedValue);

        // chua xu ly  madn *******************************************
        int madoanh_nghiep__= int.Parse(Session["madn"].ToString());

        mathang.MaDN = madoanh_nghiep__;

        mathang.ThoiGian_BatDau = DateTime.Parse(Calendar1.SelectedDate.ToString());
        //lit_Batdau.Text = Calendar1.SelectedDate.ToString();
        //mathang.ThoiGian_KetThuc = DateTime.Parse(lit_ketthuc.Text);
        mathang.ThoiGian_KetThuc = DateTime.Parse(calenketthuc.SelectedDate.ToString());
        #region "kiem tra ngay nhap vao"
        DateTime ngaybatdau = DateTime.Parse(Calendar1.SelectedDate.ToString());
        DateTime ngayketthuc = DateTime.Parse(calenketthuc.SelectedDate.ToString());
        TimeSpan dt2 = ngaybatdau.TimeOfDay;
        TimeSpan dt3 = ngayketthuc.TimeOfDay;
        if(dt2>dt3)
            {
                //lbbatdau.Visible = true;
            }
        else
            {
                //lbbatdau.Visible = false;
            }


        //if(ngayketthuc-ngaybatdau <=0)
        //    {
        //        lbngaybat_dau.Visible = true;
        //        return;
        //    }
        //else
        //    {
        //        lbngaybat_dau.Visible = false;
        //    }
        #endregion
        mathang.HinhMinhHoa = "no_images.jpg";
        

        // ban đồ

        //if (fu_bando.HasFile)
        //{
        //    string filename = DateTime.Now.Ticks.ToString() + "_" + fu_bando.FileName;
        //    fu_bando.SaveAs(Server.MapPath("~/Photos/" + filename));

        //    System.Drawing.Image img = System.Drawing.Image.FromFile(
        //        Server.MapPath("~/Photos/" + filename));

        //    ImageUtility.MakeThumbnailPhoto(
        //        img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);
        //    mathang.BanDo = filename;
        //    lbbando.Visible = false;

        //}
        //else
        //{
        //    mathang.BanDo = "no_images.jpg";
        //    lbbando.Visible = true;
        //    return;
        //}
        

       id = MATHANG_BUS.insert(mathang);
        #region"them vao bang dieu kien su dung"
        DieuKienSuDung_DTO dieukiensdung = new DieuKienSuDung_DTO();
        dieukiensdung.MaMH = id;
        dieukiensdung.LoiGioiThieu = txtloaigioithieu.Text;
        dieukiensdung.DieuKienSuDung = ftb_ddsudung.Text;
        dieukiensdung.DacDiemNoiBat = ftb_ddnoibat.Text;
        dieukiensdung.GhiChu = "chờ duyệt";
        DieuKienSuDung_BUS.Insert(dieukiensdung);

        #endregion
       //Session["mamh"] = MATHANG_BUS.insert(mathang);
        #region"chi tiet hinh anh"
      
        //HttpFileCollection hfc = Request.Files;

        //for (int i = 0; i < hfc.Count; i++)
        //{
        //    HttpPostedFile hpf = hfc[i];
        //    //HttpPostedFile mhang = hfc[0];
        //    if (hpf.ContentLength > 0)
        //    {

        //        hpf.SaveAs(Server.MapPath("~/photos/") + System.IO.Path.GetFileName(hpf.FileName));

        //        string link = System.IO.Path.GetFileName(hpf.FileName);
        //        //hinh_anh = System.IO.Path.GetFileName(hpf.FileName);

        //        System.Drawing.Image img = System.Drawing.Image.FromFile(
        //            Server.MapPath("~/photos/" + link));

        //        ImageUtility.MakeThumbnailPhoto(
        //            img, 100, 100, Server.MapPath("~/photos/thumb/"), link);

        //        HinhAnh_DTO hinhct = new HinhAnh_DTO();
        //        hinhct.Hinh = link;
        //        hinhct.MaMH = id;
        //        HinhAnh_BUS.Insert(hinhct);
        //      //  lbhinhanh.Visible = false;
        //    }
        //    else
        //    {
        //            //lbhinhanh.Visible = true;
        //            //return;
        //    }


        //    //Response.Redirect("~/admin/A_Mathang.aspx");
        //}
        #endregion
        //DieuKienSuDung_DTO dk = new DieuKienSuDung_DTO();
        //dk.DacDiemNoiBat = ftb_ddnoibat.Text;
        //dk.MaMH = id;
        //dk.DieuKienSuDung = ftb_ddsudung.Text;
        //dk.GhiChu = "Chờ duyệt";
        //DieuKienSuDung_BUS.Insert(dk);

        //catch (Exception ex)
        //{

        //}

        //hpl_themchitiethinh.Visible = true;

        //}
        //catch (Exception ex)
        //{
        //}
        //Response.Redirect("~/Admin/DanhSachSanPham.aspx");
        //hpl_themchitiethinh.NavigateUrl = "~/Admin/A_ThemHinhAnhChiTiet.aspx?mamh=" + mamh.ToString();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpFileCollection hfc = Request.Files;
         //int mamhang = int.Parse(maxmh().ToString());
        for (int i = 0; i < hfc.Count; i++)
        {
            HttpPostedFile hpf = hfc[i];
            //HttpPostedFile mhang = hfc[0];
            if (hpf.ContentLength > 0)
            {

                hpf.SaveAs(Server.MapPath("~/photos/") + System.IO.Path.GetFileName(hpf.FileName));

                string link = System.IO.Path.GetFileName(hpf.FileName);
                //hinh_anh = System.IO.Path.GetFileName(hpf.FileName);

                System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/photos/" + link));

                ImageUtility.MakeThumbnailPhoto(
                    img, 100, 100, Server.MapPath("~/photos/thumb/"), link);

                HinhAnh_DTO hinhct = new HinhAnh_DTO();
                hinhct.Hinh = link;

                hinhct.MaMH = int.Parse(maxmh().ToString());
                HinhAnh_BUS.Insert(hinhct);
                //  lbhinhanh.Visible = false;
            }
            else
            {
                //lbhinhanh.Visible = true;
                //return;
            }


            //Response.Redirect("~/admin/A_Mathang.aspx");
        }

    }
   
    protected void Button2_Click(object sender, EventArgs e)
    {
        MatHang_DTO mh = new MatHang_DTO();
        mh.MoTa = lit_mota.Text;
        mh.MaMH = int.Parse(maxmh().ToString());
        MATHANG_BUS.updatemota(mh);
    }
}
